package com.aidex.web.controller.biz;

import com.aidex.biz.domain.AppointmentCompany;
import com.aidex.biz.service.AppointmentCompanyService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 企业上门回收Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-11-22
 */
@RestController
@RequestMapping("/biz/bizAppointmentCompany")
public class AppointmentCompanyController extends BaseController {
    @Autowired
    private AppointmentCompanyService appointmentCompanyService;

    /**
     * 查询企业上门回收列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointmentCompany:list')")
    @GetMapping("/list")
    public R<PageInfo> list(AppointmentCompany appointmentCompany, HttpServletRequest request,
            HttpServletResponse response) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            appointmentCompany.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        appointmentCompany.setPage(new PageDomain(request, response));
        return R.data(appointmentCompanyService.findPage(appointmentCompany));
    }

    /**
     * 获取企业上门回收详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointmentCompany:query')")
    @GetMapping(value = "/{id}")
    public R<AppointmentCompany> detail(@PathVariable("id") String id) {
        return R.data(appointmentCompanyService.get(id));
    }

    /**
     * 新增企业上门回收
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointmentCompany:add')")
    @Log(title = "企业上门回收", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated AppointmentCompany appointmentCompany) {
        return R.status(appointmentCompanyService.save(appointmentCompany));
    }

    /**
     * 修改企业上门回收
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointmentCompany:edit')")
    @Log(title = "企业上门回收", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated AppointmentCompany appointmentCompany) {
        return R.status(appointmentCompanyService.save(appointmentCompany));
    }


    /**
     * 删除企业上门回收
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointmentCompany:remove')")
    @Log(title = "企业上门回收", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(appointmentCompanyService.deleteAppointmentCompanyByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointmentCompany:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(appointmentCompanyService.findMaxSort(new AppointmentCompany()));
    }

    /**
     * 导出企业上门回收列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizAppointmentCompany:export')")
    @Log(title = "企业上门回收", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(AppointmentCompany appointmentCompany) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            appointmentCompany.setCompanyId(null);
        } else {
            appointmentCompany.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<AppointmentCompany> list = appointmentCompanyService.findList(appointmentCompany);
        ExcelUtil<AppointmentCompany> util = new ExcelUtil<AppointmentCompany>(AppointmentCompany.class);
        return util.exportExcel(list, "企业上门回收数据");
    }

}
